package com.ls.lserp.modules.setting.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.modules.setting.dao.OgnizationDao;
import com.ls.lserp.modules.setting.entity.Ognization;
import com.ls.lserp.modules.sys.dao.BrandDao;
import com.ls.lserp.modules.sys.entity.Brand;
import com.ls.lserp.modules.sys.entity.Dict;
import com.ls.lserp.modules.sys.entity.Sequence;
import com.ls.lserp.modules.sys.utils.DictUtils;
import com.ls.lserp.modules.sys.utils.SequenceUtil;


/**
 * @ClassName: CustomerService
 * @Description: 客户信息数据保存 更新 删除
 * 
 * @author 
 * @date 2017年1月9日 下午18:40:40
 *
 */

@Service
@Transactional(readOnly = true)
public class CustomerService extends CrudService<OgnizationDao, Ognization>{
	
    @Autowired
    private OgnizationDao ognizationDao;
    
    @Autowired
    private BrandDao brandDao;
    
    /**
      * @Title: findList
      * @Description: 查询
      * @param @param page
      * @param @param ognization
      * @param @return
      * @return Page<ognization> 
      * @throws
      */
    public Page<Ognization> findList(Page<Ognization> page, Ognization ognization) {
        // 设置分页参数
    	ognization.setPage(page);
        // 执行分页查询
        page.setList(ognizationDao.findList(ognization));
        
        return page;
    }
    
    /**
     * @Title: getOgnizationData
     * @Description: 查询
     * @param @param id
     * @param @return
     * @return Ognization
     * @throws
     */
   public Ognization getOgnizationData(Long id) {
	      Ognization returnOgnization = ognizationDao.getOgnizationData(id);
	      
	      // 设定品牌
	      if(returnOgnization!=null){
	    	  if(StringUtils.isNotEmpty(returnOgnization.getCommaBrandId())){
	    		  List<Brand> returnSelBrandList = selBrandList();
	    		  String[] arrBrand = returnOgnization.getCommaBrandId().split(",");
	    		  if(!CollectionsUtils.isEmpty(returnSelBrandList)){
	    			  StringBuilder sbBrand = new StringBuilder();
	    			  for(String strBrand : arrBrand){
	    				 for(Brand brandInfo : returnSelBrandList){
	    					 if(strBrand.equals(String.valueOf(brandInfo.getId()))){
	    						 if(sbBrand.length()>0){
	    							 sbBrand.append(" ");
	    						 }
	    						 sbBrand.append(brandInfo.getBrandCn());
	    						 break;
	    					 }
	    				 }
	    			  }
	    			  
	    			  if(sbBrand.length()>0){
	    				  returnOgnization.setCommaBrandName(sbBrand.toString());
	    			  }
	    		  }
	    	  }
	      }
	   return returnOgnization;
   }
   
	/**
	  * @Title: saveCustomer
	  * @Description: 保存数据
	  * @param @param dict
	  * @return void 
	  * @throws
	  */
	@Transactional(readOnly = false)
	public void saveCustomer(Ognization ognization) {
		
		// 判断新建编辑
		//若为null 执行插入语句（新建）
		if (ognization.getId()==null){
			
			//国家取得
            Dict dictCountry=DictUtils.getDictEntity(ognization.getOgnizationCountry());
			//城市取得
            Dict dictCity=DictUtils.getDictEntity(ognization.getOgnizationCity());
			//客户类别取得
            Dict dictCategory=DictUtils.getDictEntity(ognization.getOgnizationCategoryCd());
			//客户级别取得
            Dict dictLevel=DictUtils.getDictEntity(ognization.getOgnizationLevelCd());
            
            StringBuilder conCode = new StringBuilder();
            conCode.append(dictCountry.getItemShortNameEn());
            conCode.append(dictCity.getItemShortNameEn());
            conCode.append(dictCategory.getItemShortNameEn());
            conCode.append(DateUtils.formatDate(new Date(), "yyyy"));
            conCode.append(dictLevel.getItemShortNameEn());
            // 店铺编号
            Sequence sequence=SequenceUtil.ognizationCode(conCode.toString(), "sequence_customer", 4);
            ognization.setOgnizationCode(sequence.getReturnCode());
			
			
			ognization.preInsert();
			ognizationDao.insert(ognization);
			Long ognId = ognization.getId();
			Ognization companyInfo = ognizationDao.getCompanyId(ognization.getCreateBy().getId());
			ognization.setCompanyId(companyInfo.getCompanyId());
			ognizationDao.insertOgnR(ognization);
			ognization.setId(ognId);
			ognizationDao.insertBankAccount(ognization);
			ognization.setId(ognId);
			ognizationDao.insertOgnizationBrand(ognization);
		// 编辑
		}else{
			// 更新数据
			ognization.preUpdate();
			ognizationDao.update(ognization);
			ognizationDao.updateBankAccount(ognization);
			ognizationDao.updateOgnizationBrand(ognization);
		}
	}
	
    /**
     * @Title: selBrandList
     * @Description: 查询
     * @param @return
     * @return List<Brand>
     * @throws
     */
   public List<Brand> selBrandList() {
   
	   return brandDao.findList(new Brand());
   }
   
  /**
   * 逻辑删除
   * @throws
   */
   @Transactional(readOnly = false)
	 public void delete(Ognization ognization) {
	 
		 ognizationDao.delete(ognization);
	 }
}
